<template>
  <div>
    <el-dialog :title="isAdd?'添加菜单':'编辑菜单'" :visible.sync="isShow" :before-close="cancel" center>
      <el-form ref="menu" :model="menuInfo" :rules="rules">
        <el-form-item prop="title" label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="menuInfo.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">
          <!-- 下拉框 -->
          <el-select  @change="changePid" v-model="menuInfo.pid" placeholder="请选择上级菜单">
            <el-option label="上级菜单" :value="0"></el-option>
            <el-option v-for="item in menulist" :key="item.id" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio disabled v-model="menuInfo.type" :label="1">目录</el-radio>
          <el-radio disabled v-model="menuInfo.type" :label="2">菜单</el-radio>
        </el-form-item>

        <el-form-item v-if="menuInfo.type==1" label="菜单图标" :label-width="formLabelWidth">
          <el-select v-model="menuInfo.icon" placeholder="请选择">
            <el-option
              v-for="item in iconlist"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
          <el-select v-model="menuInfo.url" placeholder="请选择">
            <el-option
              v-for="item in indexRoutes"
              :key="item.path"
              :label="item.meta.title"
              :value="item.path"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="isAdd" type="primary" @click="addInfo">添 加</el-button>
        <el-button v-else type="primary" @click="editInfo">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { menuadd, menuinfo, menuedit } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
import { indexRoutes } from '../../router';

export default {
  data() {
    return {
      menuInfo: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1
      },
      formLabelWidth: "120px",
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8个字符", trigger: "blur" }
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }]
      },
      iconlist:['el-icon-s-tools','el-icon-s-goods'],
      indexRoutes
    };
  },
  props: ["isShow", "isAdd"],
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu"
    })
  },
  methods: {
    ...mapActions({ getMenu: "menu/getMenuActions" }),
    changePid(){
      this.menuInfo.type = this.menuInfo.pid==0?1:2
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    reset() {
      this.menuInfo = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1
      };
      //清空表单验证
      this.$refs.menu.resetFields();
    },
    addInfo() {
      this.$refs.menu.validate(val => {
        if (val) {
          console.log(this.menuInfo, "添加成功");
          menuadd(this.menuInfo).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              this.getMenu();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("验证失败");
        }
      });
    },
    lookup(id) {
      console.log(id, "列表id");
      menuinfo({ id }).then(res => {
        console.log(res, "查询一条数据");
        if (res.code == 200) {
          this.menuInfo = res.list;
          this.menuInfo.id = id;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    editInfo() {
      this.$refs.menu.validate(val => {
        if (val) {
          console.log(this.menuInfo, "添加成功");
          menuedit(this.menuInfo).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              this.getMenu();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("验证失败");
        }
      });
    }
  }
};
</script>

<style  scoped>
</style>
